package com.arturjaneczko.view.impl.clipboard.preview._cell;

import java.awt.Color;
import java.awt.Component;

import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

import com.arturjaneczko.model.impl.EClipboardDataType;
import com.arturjaneczko.view.impl.clipboard._component.ClipboardObjectPreviewView;

public class PreviewCell extends AbstractCellEditor implements TableCellEditor,
		TableCellRenderer {

	private ClipboardObjectPreviewView clipboardObjectPreviewView;

	public PreviewCell(EClipboardDataType clipboardDataType) {
		super();
		clipboardObjectPreviewView = new ClipboardObjectPreviewView(clipboardDataType);
	}

	private void update(JTable table, boolean isSelected) {
		if (isSelected) {
			clipboardObjectPreviewView.setBackground(table
					.getSelectionBackground());
		} else {
			clipboardObjectPreviewView.setBackground(table.getBackground());
		}
	}

	private void update(JTable table, boolean isSelected, boolean hasFocus) {
		if (hasFocus) {
			clipboardObjectPreviewView.setBackground(Color.cyan);
		} else if (isSelected) {
			clipboardObjectPreviewView.setBackground(table
					.getSelectionBackground());
		} else {
			clipboardObjectPreviewView.setBackground(table.getBackground());
		}
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		update(table, isSelected, hasFocus);
		return clipboardObjectPreviewView;
	}

	@Override
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) {
		update(table, isSelected);
		return clipboardObjectPreviewView;
	}

	@Override
	public Object getCellEditorValue() {
		return null;
	}

}
